package com.jx.medical.controller;

import com.jx.medical.pojo.AjaxInfo;
import com.jx.medical.pojo.Buyer;
import com.jx.medical.pojo.User;
import com.jx.medical.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @author jx
 * @date Created on 2020/12/3  15:53
 **/

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public AjaxInfo login(@RequestBody User user, HttpSession session) {

        AjaxInfo ajaxInfo = new AjaxInfo();
        System.out.println(session.getId());
        User user1 = userService.findUser(user);
        if (user1 != null) {
            ajaxInfo.setCode(user1.getUserRight());
            ajaxInfo.setMsg("登陆成功！");
            ajaxInfo.setData(user1.getUserName());
            session.setAttribute("userName", user1.getUserName());
            System.out.println(session.getAttribute("userName"));
        } else {
            ajaxInfo.setCode(-1);
            ajaxInfo.setMsg("账号或密码错误！");
        }
        return ajaxInfo;
    }

    @RequestMapping("/logout")
    public String login(HttpSession session) {

        System.out.println(session.getId());
        System.out.println(session.getAttribute("userName"));
        session.removeAttribute("userName");
        return "success";
    }

    @RequestMapping("/registUser")
    public AjaxInfo regist(@RequestBody User user, HttpSession session) {
        AjaxInfo ajaxInfo = new AjaxInfo();

        User userByAcc = userService.getUserByAcc(user.getUserAcc());
        if (session.getAttribute("userName") != null) {
            if (userByAcc == null) {
                userService.registUser(user);
                ajaxInfo.setMsg("注册成功！");
            } else {
                ajaxInfo.setMsg("账号已存在！请重新输入~");
            }
            return ajaxInfo;
        } else {
            ajaxInfo.setMsg("权限不足！请先登录~");
        }
        return ajaxInfo;
    }

    @RequestMapping("/queryUser")
    public AjaxInfo queryUser(@RequestBody User user, HttpSession session) {
        System.out.println(user);
        AjaxInfo ajaxInfo = new AjaxInfo();

        if (session.getAttribute("userName") != null) {
            User userByAcc = userService.getUserByAcc(user.getUserAcc());
            if (userByAcc == null||userByAcc.getUserRight()!=2) {
                ajaxInfo.setMsg("该取药员不存在，请重新输入！");
                ajaxInfo.setCode(-1);
            } else {
                ajaxInfo.setMsg("查询成功！");
                ajaxInfo.setCode(0);
                ajaxInfo.setData(userByAcc);
            }
            return ajaxInfo;
        } else {
            ajaxInfo.setCode(-2);
            ajaxInfo.setMsg("权限不足！请先登录~");
        }
        return ajaxInfo;
    }

    @RequestMapping("/deleteUser")
    public AjaxInfo deleteUser(@RequestBody User user, HttpSession session) {
        AjaxInfo ajaxInfo = new AjaxInfo();

        if (session.getAttribute("userName") != null) {
            userService.deleteUser(user.getUserAcc());
            ajaxInfo.setMsg("删除成功！");
        } else {
            ajaxInfo.setMsg("权限不足！请先登录~");
        }
        return ajaxInfo;
    }

    @RequestMapping("/registBuyer")
    public AjaxInfo registBuyer(@RequestBody Buyer buyer, HttpSession session) {
        AjaxInfo ajaxInfo = new AjaxInfo();
        System.out.println("======"+buyer);
        Buyer buyerByUserId = userService.getBuyerById(buyer.getBuyerId());
        if (session.getAttribute("userName") != null) {
            if (buyerByUserId == null) {
                userService.registBuyer(buyer);
                ajaxInfo.setMsg("注册成功！");
            } else {
                ajaxInfo.setMsg("ID已存在！");
            }
            return ajaxInfo;
        } else {
            ajaxInfo.setMsg("权限不足！请先登录~");
        }
        return ajaxInfo;
    }

    @RequestMapping("/queryBuyer")
    public AjaxInfo queryBuyer(@RequestBody Buyer buyer, HttpSession session) {
        AjaxInfo ajaxInfo = new AjaxInfo();
        System.out.println(buyer);

        if (session.getAttribute("userName") != null) {
            Buyer buyer1 = userService.getBuyerById(buyer.getBuyerId());
            if (buyer1 == null) {
                ajaxInfo.setMsg("该采购员不存在，请重新输入！");
                ajaxInfo.setCode(-1);
            } else {
                ajaxInfo.setMsg("查询成功！");
                ajaxInfo.setData(buyer1);
                ajaxInfo.setCode(0);
            }

        } else {
            ajaxInfo.setCode(-2);
            ajaxInfo.setMsg("权限不足！请先登录~");
        }
        System.out.println("================="+ajaxInfo.getCode());
        return ajaxInfo;
    }

    @RequestMapping("/deleteBuyer")
    public AjaxInfo deleteBuyer(@RequestBody Buyer buyer, HttpSession session) {
        AjaxInfo ajaxInfo = new AjaxInfo();
        if (session.getAttribute("userName") != null) {
            userService.deleteBuyer(buyer.getBuyerId());
            ajaxInfo.setMsg("删除成功！");
        } else {
            ajaxInfo.setMsg("权限不足！请先登录~");
        }
        return ajaxInfo;
    }

    @RequestMapping("/updateBuyer")
    public AjaxInfo updateBuyer(@RequestBody Buyer buyer, HttpSession session) {
        AjaxInfo ajaxInfo = new AjaxInfo();
        if (session.getAttribute("userName") != null) {
            userService.updateBuyer(buyer);
            ajaxInfo.setMsg("更新成功！");
        } else {
            ajaxInfo.setMsg("权限不足！请先登录~");
        }
        return ajaxInfo;
    }

}
